package com.locationtracker.plus.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.locationtracker.plus.app.network.apiService
import com.locationtracker.plus.data.model.bean.UserInfo
import com.locationtracker.plus.data.repository.HttpRequestCoroutine
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

class RequestLoginRegisterViewModel : BaseViewModel() {

    var loginResult = MutableLiveData<ResultState<UserInfo>>()
    var getCodeStatus = MutableLiveData<Long>()

    fun loginReq(phone: String, code: String) {
        request(
            { HttpRequestCoroutine.loginByCode(phone, code) }, loginResult,
            true,
            "正在登录中..."
        )
    }

    fun getCode(phone: String, countryCode: String) {
        request(
            { apiService.sendCode(phone, countryCode) }, {
                getCodeStatus.value = System.currentTimeMillis()
            }, {
                ToastUtils.showShort(it.errorMsg)
            },
            true
        )
    }
}